package com.zirenx.sys.controller;

import java.util.ArrayList;
import java.util.List;

import com.jfinal.aop.Inject;
import com.zirenx.annotation.RouteMapping;
import com.zirenx.common.controller.BaseController;
import com.zirenx.common.controller.IController;
import com.zirenx.sys.model.SysMenu;
import com.zirenx.sys.service.SysMenuService;
import com.zirenx.utils.LogUtils;
import com.zirenx.utils.R;
import com.zirenx.utils.StringUtils;
import com.zirenx.utils.tree.Tree;

@RouteMapping("/sys/menu")
public class SysMenuController extends BaseController implements IController{
	@Inject
	SysMenuService sysMenuService;
	
	public void index() {
		render("index.html");
	}
	
	public void add() {
		String parentId = get("parentId");
		
		SysMenu parent = new SysMenu();
		parent.setId(0l);
		parent.setName("顶级菜单");
		parent.setType(SysMenu.TypeEnum.目录.getValue());
		if(!StringUtils.isEmpty(parentId)) {
			SysMenu bean = sysMenuService.findById(parentId);
			if(bean!=null) {
				parent = bean;
				if(parent.getType()!=null && parent.getType().intValue() < SysMenu.TypeEnum.按钮.getValue()) {
					parent.setType(parent.getType().intValue()+1);
				}
			}
		}
		
		setAttr("parent", parent);
		setAttr("typeList", SysMenu.TypeEnum.getAll());
		render("add.html");
	}
	
	public void edit() {
		SysMenu bean = sysMenuService.findById(get("id"));
		Long parentId = bean.getParentId();
		String parentName = "顶级菜单";
		if(parentId!=null && parentId.longValue()>0) {
			SysMenu sysMenu = sysMenuService.findById(parentId+"");
			parentName = sysMenu.getName();
		}
		setAttr("bean", bean);
		setAttr("parentName", parentName);
		setAttr("typeList", SysMenu.TypeEnum.getAll());
		
		render("edit.html");
	}
	
	public void dataAjax() {
		List<SysMenu> list = new ArrayList<SysMenu>();
		try {
			list = sysMenuService.list();
			for (SysMenu sysMenu : list) {
				if(sysMenu.getType()!=null) {
					sysMenu.setTypeName(SysMenu.TypeEnum.getByValue(sysMenu.getType().intValue()).getName());
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		renderJson(list);	
	}
	
	public void batchRemove() {
		try {
			String[] ids = getParaValues("ids");
			if(ids!=null && ids.length>0) {
				for (String id : ids) {
					if(id!=null && !id.equals("")) {
						sysMenuService.delete(id);
					}
				}
			}
			renderJson(R.ok());
		} catch (Exception e) {
			e.printStackTrace();
			renderJson(R.error());
		}
	}

	@Override
	public void save() {
		getBean(SysMenu.class,"bean").save();
		renderJson(R.ok());
	}

	@Override
	public void update() {
		getBean(SysMenu.class,"bean").update();
		renderJson(R.ok());
	}

	@Override
	public void remove() {
		try {
			sysMenuService.delete(get("id"));
			renderJson(R.ok());
		}catch (Exception e){
			LogUtils.error(this.getClass(),e);
			renderJson(R.error());
		}
	}

	/**
	 * 获取树结构数据
	 */
	public void tree(){
		Tree<SysMenu> tree = new Tree<SysMenu>();
		try {
			String roleId = get("roleId");
			if(StringUtils.isEmpty(roleId)){
				tree = sysMenuService.getTree();
			}else {
				tree = sysMenuService.getTree(roleId);
			}
		}catch (Exception e){
			LogUtils.error(this.getClass(),e);
		}
		renderJson(tree);
	}

}
